package com.jieli.bt.att.demo

import android.bluetooth.BluetoothDevice
import com.jieli.bt.att.data.constant.Config
import com.jieli.bt.att.tool.ble.BleManager
import com.jieli.bt.att.tool.ble.interfaces.BleEventCallback
import com.jieli.bt.att.tool.ble.interfaces.OnWriteDataCallback
import java.util.UUID

/**
 * IODemo
 * @author zqjasonZhong
 * @since 2025/3/31
 * @email zhongzhuocheng@zh-jieli.com
 * @desc
 */
class IODemo {

    fun sendData(device: BluetoothDevice, data: ByteArray) {
        //初始化BleManager对象
        val bleManager = BleManager.getInstance()
        //异步发送数据
        bleManager.writeDataByBleAsync(
            device,
            Config.BLE_SERVICE_UUID,
            Config.BLE_WRITE_UUID,
            data,
            object : OnWriteDataCallback {
                override fun onBleResult(
                    device: BluetoothDevice?,
                    serviceUUID: UUID?,
                    characteristicUUID: UUID?,
                    result: Boolean,
                    data: ByteArray?
                ) {
                    //发送数据的回调
                }
            })
    }


    fun receivedData() {
        //初始化BleManager对象
        val bleManager = BleManager.getInstance()
        //注册连接状态回调
        val callback = object : BleEventCallback() {
            override fun onBleDataNotification(
                device: BluetoothDevice?,
                serviceUuid: UUID?,
                characteristicsUuid: UUID?,
                data: ByteArray?
            ) {
                //回调接收到的数据
                if (Config.BLE_SERVICE_UUID == serviceUuid && Config.BLE_NOTIFY_UUID == characteristicsUuid) {
                    //可以对数据进行过滤
                }
            }
        }
        bleManager.registerBleEventCallback(callback)
        //不需要监听连接状态时，记得移除监听器
        //bleManager.unregisterBleEventCallback(callback)
    }
}